import Vue from 'vue'
import Vuex from 'vuex'


import { getuserInfo } from '@/api/user.js'
Vue.use(Vuex)
const CHANGE = "change"
export default new Vuex.Store({
  state: {
    token: '',
    uid: '',
    userInfo: {},
    isLogin:false,
    publish_num:0,
    liked_num:0,
  },
  getters: {
  },
  // 改数据
  mutations: {
    [CHANGE](state, { key, value }) {
      state[key] = value
    },
    clear(state){
      state.token='',
      state.uid='',
      state.userInfo= {}
      state.isLogin= false,
      state.publish_num=0,
      state.liked_num=0
    }
  },
  // 方法
  actions: {
   async getuserInfo({ commit }, token) {


   return   getuserInfo({ token }).then(res => {
        console.log(res)
        // console.log(res.userInfo)
        // this.state.isLogin=true
        commit("change", { key: "userInfo", value: res.userInfo })
        commit('change', { key: "uid", value: res.uid })
        commit('change', { key: "token", value:token })
        commit('change', { key: "isLogin", value:true })
        commit('change', { key: "liked_num", value:res.liked_num })
        commit('change', { key: "publish_num", value:res.publish_num })
      })
   
    },
  
   
  },
  modules: {
  }
})
